import * as utils from '../utils'

function createCourse07 (gl: WebGLRenderingContext) {
  gl.clearColor(0.0, 0.0, 0.0, 1.0)
  gl.clear(gl.COLOR_BUFFER_BIT)
  const vectexShaderSrc = `
    attribute vec4 a_Position;
      void main() {
          gl_Position = a_Position;
      }
    `

  const fragmentShaderSrc = `
    precision mediump float;
    uniform vec4 a_FragColor;
      void main() {
          gl_FragColor  = a_FragColor;
      }
    `
  const program = utils.initShaders(gl, vectexShaderSrc, fragmentShaderSrc)

  function initVertexBuffers () {
    // 将attribute变量跟javascript变量绑定在一起
    const aPosition = gl.getAttribLocation(program, 'a_Position')

    const vertices = new Float32Array([
      -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5
    ])
    // 创建缓冲区对象
    const vertexBuffer = gl.createBuffer()
    // 绑定缓冲区
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer)
    // 向缓冲区写入对象
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW)
    // 将缓冲区对象分配个attribute变量
    gl.vertexAttribPointer(aPosition, 2, gl.FLOAT, false, 0, 0)
    // 开启attribute变量
    gl.enableVertexAttribArray(aPosition)

    return 4
  }

  const count = initVertexBuffers()

  gl.drawArrays(gl.TRIANGLE_FAN, 0, count)
}

export {
  createCourse07
}
